﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Character_rev
    {
        string[,] _character_rev;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._character_rev = Query.SelectQuery(this.conn, "character_rev", column);
        }

        public string[,] Rev
        {
            get { return this._character_rev; }
        }

        public void setPositionByName(string name, int revxsec, int revysec, int revx, int revy, int revz)
        {
            string[] columns = { "revxsec", "revysec", "revx", "revy", "revz" };
            string[] values = { revxsec.ToString(), revysec.ToString(), revx.ToString(), revy.ToString(), revz.ToString() };
            Query.UpdateQuery(this.conn, "character_rev", columns, values, "charname=" + name);
        }

        public void setPositionById(int id, int revxsec, int revysec, int revx, int revy, int revz)
        {
            string[] columns = { "revxsec", "revysec", "revx", "revy", "revz" };
            string[] values = { revxsec.ToString(), revysec.ToString(), revx.ToString(), revy.ToString(), revz.ToString() };
            Query.UpdateQuery(this.conn, "character_rev", columns, values, "id=" + id);
        }

        public int[] getPositionByName(string name, bool refresh = false)
        {
            if (refresh)
                this.refresh();
            int[] pos = new int[5];
            int arraypos = 0;
            for (int i = 0; i < this._character_rev.GetLength(0); i++)
            {
                if(this._character_rev[i, 1] == name)
                {
                    arraypos = i;
                    break;
                }
            }

            for (int i = 0; i < pos.Length; i++)
            {
                Int32.TryParse(this._character_rev[arraypos, i], out pos[i]);
            }

            return pos;
        }

        public int[] getPositionByName(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();
            int[] pos = new int[5];

            for (int i = 0; i < pos.Length; i++)
            {
                Int32.TryParse(this._character_rev[id - 1, i], out pos[i]);
            }

            return pos;
        }
    }
}
